<?php defined('SYSPATH') or die('No direct script access.');
 
class Model_Works extends ORM {
	protected $_table_name = 'works';

	public function getLast($postfix) {
		return DB::select('id', array('title'.$postfix, 'title'), 'image', 'creation_date')
			->from($this->_table_name)
			->order_by('creation_date', 'DESC')
			->limit(3)
			->as_object()
			->execute();
	}

	public function getAllYear($postfix, $offset, $year) {
		$items_per_page = Kohana::$config->load('main')->works_per_page;

		return DB::select('id', array('title'.$postfix, 'title'), array('task'.$postfix, 'task'), 'image', 'creation_date')
			->from($this->_table_name)
			->where(DB::expr('YEAR(`creation_date`)'), '=', $year)
			->order_by('creation_date', 'DESC')
			->limit($items_per_page)
			->offset($offset)
			->as_object()
			->execute();
	}

	public function getAll($postfix, $offset) {
		$items_per_page = Kohana::$config->load('main')->works_per_page;

		return DB::select('id', array('title'.$postfix, 'title'), array('task'.$postfix, 'task'), 'image', 'creation_date')
			->from($this->_table_name)
			->order_by('creation_date', 'DESC')
			->limit($items_per_page)
			->offset($offset)
			->as_object()
			->execute();
	}

	public function getOne($postfix, $id) {
		return DB::select('id', array('title'.$postfix, 'title'), array('task'.$postfix, 'task'), array('meta_description'.$postfix, 'meta_description'), 'link')
			->from($this->_table_name)
			->where('id', '=', $id)
			->as_object()
			->execute()
			->current();
	}

	public function getDates() {
		return DB::query(Database::SELECT, 'SELECT DISTINCT YEAR(`creation_date`) AS `creation_date` FROM `works` ORDER BY `creation_date` DESC')
			->as_object()
			->execute();
	}

}